package net.aimeizi.quartz.controller;

import net.aimeizi.quartz.service.ScheduleJobService;
import net.aimeizi.quartz.utils.JsonResult;
import net.aimeizi.quartz.vo.ScheduleJobVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "schedule_job")
public class ScheduleJobApiController {

    /**
     * job service
     */
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 删除任务
     *
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult delete(Long scheduleJobId) {
        try {
            scheduleJobService.delete(scheduleJobId);
            return JsonResult.build().success();
        } catch (Exception e) {
            return JsonResult.build().fail(e.getMessage());
        }
    }

    /**
     * 运行一次
     *
     * @return
     */
    @RequestMapping(value = "runOnce", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult runOnce(Long scheduleJobId) {
        try {
            scheduleJobService.runOnce(scheduleJobId);
            return JsonResult.build().success();
        } catch (Exception e) {
            return JsonResult.build().fail(e.getMessage());
        }
    }

    /**
     * 暂停
     *
     * @return
     */
    @RequestMapping(value = "pause", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult pause(Long scheduleJobId) {
        try {
            scheduleJobService.pauseJob(scheduleJobId);
            return JsonResult.build().success();
        } catch (Exception e) {
            return JsonResult.build().fail(e.getMessage());
        }
    }

    /**
     * 恢复
     *
     * @return
     */
    @RequestMapping(value = "resume", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult resumeScheduleJob(Long scheduleJobId) {
        try {
            scheduleJobService.resumeJob(scheduleJobId);
            return JsonResult.build().success();
        } catch (Exception e) {
            return JsonResult.build().fail(e.getMessage());
        }
    }

    /**
     * 保存任务
     *
     * @param scheduleJobVo
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult saveScheduleJob(ScheduleJobVo scheduleJobVo) {
        try {
            if (scheduleJobVo.getScheduleJobId() == null) {
                scheduleJobService.insert(scheduleJobVo);
            } else {
                scheduleJobService.delUpdate(scheduleJobVo);
            }
            return JsonResult.build().success();
        } catch (Exception e) {
            return JsonResult.build().fail(e.getMessage());
        }
    }

    //任务列表页
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listScheduleJob(ScheduleJobVo scheduleJobVo) {

        try {
            List<ScheduleJobVo> scheduleJobVoList = scheduleJobService.queryList(scheduleJobVo);
            return JsonResult.build().success(scheduleJobVoList);
        } catch (Exception e) {
            return JsonResult.build().fail(e.getMessage());
        }
    }

}
